package default_package;
import java.io.*;

/**
 * Class to read and parse the input from the standard input stream.
 * @author JeroenGillis
 *
 */
public class InputParser {
	
public InputParser() {
	
}

/**
 * Reads a line from input and returns the typed string.
 * @return	The string that has been read.
 */
public static String readLine() {
	try {
	BufferedReader reader = new BufferedReader(new InputStreamReader(System.in));
	return reader.readLine();
	}
	catch (IOException e) {
		System.out.println(e);
		return null;
	}
}

/**
 * Reads a line and parses it on " ".
 * @return	An array containing the parsed input.
 */
public static String[] readLineAndParse() 
{
	String input = readLine();
	return parse(input);
}

/**
 * Parses the input on " ".
 * @param input	The string that needs to be parsed
 * @return	Returns an array containing the different words of the string
 */
public static String[] parse(String input) {
	if(input == null)
		return null;
	String[] words = input.split(" ");
	return words;
}

/**
 * Returns the host from a given uri
 * @param uri	The uri
 * @return	The host
 */
public static String getHost(String uri) {
	return (uri.split("/"))[0];
}
/**
 * This method gets splits the filepath from the entire uri
 * @param uri	The entire uri
 * @param host	The host
 * @return	A string with the relative filepath
 */
public static String getFilePath(String uri, String host) {
	if(uri.startsWith(host))
		return uri.substring(uri.indexOf(host) + host.length() + 1);
	else if(uri.startsWith("/"))
		return uri.substring(1);
	else
		return uri;
}

public static String getHTTPVersion(String input) {
	String version = parse(input)[3];
	return version;
}
public static String[] getImageURI(String input) {
	String[] splittedText = input.split("<img src=\"");
	System.out.println("<img src=\"");
	if(splittedText.length > 1) {
		int i = 1;
		String[] imageURI = new String[splittedText.length-1];
		while (splittedText.length > i-1) {
			imageURI[i-1] = (splittedText[1].split("\""))[0];
		}
		return imageURI;
	}
	else
		return null;
}
}
